<?php


use Illuminate\Database\Capsule\Manager as DB;

class Dianpu_ManageController extends CommonController
{
    #[获取下级订单]
    public function getordersAction()
    {
        if (!$this->isPost()){
            ret(0, '请求出错！');
        }
        $data = [];
        $type = $this->get('type', 'all');
        $date_type = $this->get('date_type', 'today');
        $date = $this->get('date', []);
        if(empty($date)){
            switch ($date_type){
                case 'yesterday':
                    $date[0] = carbon()->yesterday()->format('Y-m-d');;
                    $date[1] = carbon()->yesterday()->format('Y-m-d');;
                    break;
                case 'week':
                    $date[0] = carbon()->startOfWeek()->format('Y-m-d');
                    $date[1] = carbon()->today()->format('Y-m-d');
                    break;
                case 'month':
                    $date[0] = carbon()->startOfMonth()->format('Y-m-d');
                    $date[1] = carbon()->today()->format('Y-m-d');
                    break;
                default:
                case 'today':
                    $date[0] = carbon()->today()->format('Y-m-d');
                    $date[1] = carbon()->today()->format('Y-m-d');
            }
        }
        $query = dianpurewardModel::query()
                                  ->with(['buyerInfo', 'orderInfo'=>function($orderWith){
                                      $orderWith->with(['item']);
                                  }])
                                  ->withSum('orderInfo', 'total_fee')
                                  ->where('manager_id', $this->user->id)
                                  ->where('status', '>=', 0)->whereHas('orderInfo', function ($orderWhere) use($date, $type){
                                    if(isset($date[0])){
                                      $orderWhere->whereDate('created_at', '>=', $date[0]);
                                    }
                                    if(isset($date[1])){
                                      $orderWhere->whereDate('created_at', '<=', $date[1]);
                                    }
                                    switch ($type) {
                                        case 'nosend':
                                            $orderWhere->payed()->nosend();
                                            break;
                                        case 'noget':
                                            $orderWhere->payed()->noget();
                                            break;
                                        case 'finish':
                                            $orderWhere->payed()->finish();
                                            break;
                                        default:
                                            break;
                                  }
            })->groupBy('order_id');
        $total_sql = $query->toSql();
        $total = DB::table(DB::Raw("($total_sql) as total_order"))->mergeBindings($query->getQuery())->count('id');
        $page = $this->get('page', 1);
        $query = $query->offset(($page - 1) * 10)
                      ->limit(10);
        $rows = $query->get();
        $data['rows'] = [];
        $data['total_money'] = 0;
        if($rows){
            $rows->each(function ($item, $key) {
                $item->orderInfo->append('status_code');
                $item->orderInfo->append('status_name');
                $item->orderInfo->append('status_desc');
            });
            // 处理未支付订单 item status_code

            $data['rows'] = $rows->toArray();
            foreach ($data['rows'] as &$row){
                $data['total_money'] = bcadd($data['total_money'], $row['order_info_sum_total_fee'], 2);
                $row['order_info'] = orderModel::setOrderItemStatusByOrder($row['order_info']);
            }
        }
        $data['total'] = $total;
        json(['code'=>1,'msg'=>'获取成功', 'data'=>$data]);
    }
}